package com.runchen.broadcastdetection.ui.activity.login;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.runchen.broadcastdetection.Constants;
import com.runchen.broadcastdetection.R;
import com.runchen.broadcastdetection.databinding.ActivityLoginBinding;
import com.runchen.broadcastdetection.mvp.login.LoginCovenant;
import com.runchen.broadcastdetection.mvp.login.LoginPresenter;
import com.runchen.broadcastdetection.ui.activity.home.MainActivity;
import com.runchen.broadcastdetection.utils.SoftKeyboardFixerForFullscreen;
import com.ruochen.common.BuildConfig;
import com.ruochen.common.base.BaseMvpActivity;
import com.ruochen.common.entity.UserInfo;
import com.ruochen.common.listener.NoDoubleClickListener;
import com.ruochen.common.utils.SerializableSpTools;

import java.util.UUID;

/**
 * @Description 登录
 * @Auth RuoChen
 * @Date 04/04/2021, 14:20
 **/
public class LoginActivity extends BaseMvpActivity<ActivityLoginBinding, LoginPresenter> implements LoginCovenant.MvpView {

    private String tempUUID;

    @Override
    protected void setTitleBar(Bundle savedInstanceState) {
        titleBar.setTitleMainText("登录");
    }

    @Override
    protected void initView(Bundle var1) {
        SoftKeyboardFixerForFullscreen.assistActivity(this);

        generateCode();
    }

    @Override
    protected void afterSetView(Bundle savedInstanceState) {
        viewBinding.ivCode.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                generateCode();
            }
        });
        viewBinding.rtvDoLogin.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                mvpPresenter.login();
            }
        });
    }

    public void generateCode() {
        tempUUID = UUID.randomUUID().toString();
        viewBinding.ivCode.setEnabled(false);
        viewBinding.pbCodeLoading.setVisibility(View.VISIBLE);
        Glide.with(mContext)
                .load(BuildConfig.SERVER_HOST + String.format(Constants.URL_GET_CODE, tempUUID))
                .placeholder(R.color.colorBackground)
                .error(R.color.colorBackground)
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        viewBinding.pbCodeLoading.setVisibility(View.GONE);
                        viewBinding.ivCode.setEnabled(true);
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        viewBinding.pbCodeLoading.setVisibility(View.GONE);
                        viewBinding.ivCode.setEnabled(true);
                        return false;
                    }
                })
                .into(viewBinding.ivCode);
    }

    @Override
    protected LoginPresenter createPresenter() {
        return new LoginPresenter(this);
    }


    @Override
    public void onLoginSuccess(UserInfo userInfo) {
        showToast("登录成功");
        SerializableSpTools.putUserInfo(userInfo);
        SerializableSpTools.setToken(userInfo.getToken());
        SerializableSpTools.setIsLogin(true);
        startActivity(MainActivity.class);
    }

    @Override
    public void onLoginFailure(int code, String msg) {
        showToast(msg);
        generateCode();
    }

    @Override
    public String getAccountText() {
        return viewBinding.etAccount.getText().toString().trim();
    }

    @Override
    public String getPasswordText() {
        return viewBinding.etPassword.getText().toString().trim();
    }

    @Override
    public String getCaptchaText() {
        return viewBinding.etCode.getText().toString().trim();
    }

    @Override
    public String getUUIDText() {
        return tempUUID;
    }
}
